﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Web.Script.Serialization;

using DataAccessEntLib;

public partial class Post_Ad : System.Web.UI.Page
{
    BusinessLogic busin = new BusinessLogic();
    BO.Ad bo = new BO.Ad();
    BLL.AdMagnager bll = new BLL.AdMagnager();
    RegistrationData _objRegistrationData = new RegistrationData();
    protected int   FeaturedAdsLimit = 0, TopAdsLimit = 0, HomePageGalery = 0;
    string ImageName = string.Empty, folderPath = string.Empty, ImagePath = string.Empty;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCategory();
        }
    }

    protected void BindCategory()
    {
        IDataReader dr;
        ddlCategory.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlCategory.Items.Add(lst1);
        dr = bll.SelectAdCategoryMaster();
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["AdCategory"].ToString();
                lst.Value = dr["AdCategoryID"].ToString();
                ddlCategory.Items.Add(lst);
            }
        }
        dr.Close();
    }
 
    protected void BindSubCategory()
    {
        IDataReader dr;
        ddlSubCategory.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlSubCategory.Items.Add(lst1);
        if (ddlCategory.SelectedItem.Text != "Select")
        {
            bo.AdCategoryID = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
        }
        else
        {
            bo.AdCategoryID = 0;
        }
        dr = bll.ShowSubCategoryMasterDetails(bo);
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["SubCategory"].ToString();
                lst.Value = dr["SubCategoryID"].ToString();
                ddlSubCategory.Items.Add(lst);
            }
        }
        dr.Close();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["User"] != null)
        {
            _objRegistrationData = (RegistrationData)Session["User"];
            bo.UserID = _objRegistrationData.UserID;
        }
        if (ValidatePage())
        {
            try
            {
                if (chkFeaturedAd.Checked)
                    bo.FeaturedAd = 1;
                else
                    bo.FeaturedAd = 0;

                if (chkTopAd.Checked)
                    bo.TopAd = 1;
                else
                    bo.TopAd = 0;

                if (chkHomepageGallery.Checked)
                    bo.HomepageGallery = 1;
                else
                    bo.HomepageGallery = 0;

                if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
                {
                    ImageName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    folderPath = "AdImages/";
                    ImagePath = folderPath + ImageName;

                    ImageName = busin.ValidateImageName(folderPath, ImageName);
                    ImagePath = folderPath + ImageName;
                    FileUpload1.PostedFile.SaveAs(Server.MapPath(ImagePath));
                    bo.ImagePath = ImagePath;
                }
                else
                {
                    bo.ImagePath = GetImagePath();
                }

                //bo.UserID = 0;
                if (ddlCategory.SelectedItem.Text != "Select")
                    bo.AdCategoryID = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
                else
                    bo.AdCategoryID = 0;

                if (ddlSubCategory.SelectedItem.Text != "Select")
                    bo.SubCategoryID = Convert.ToInt32(ddlSubCategory.SelectedValue.ToString());
                else
                    bo.SubCategoryID = 0;

                bo.AdTitle = txtTitle.Text;
                bo.ProductDescription = txtDescription.Text;
                bo.LandMark = txtLocal.Text;
                bo.State = txtState.Text;
                bo.Country = "India";

                int strResult = 0;
                strResult = bll.InsertAdMaster(bo);
                if (strResult > 0)
                {

                    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
                    {
                        ImageName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                        folderPath = "AdImages/";
                        ImagePath = folderPath + ImageName;

                        ImageName = busin.ValidateImageName(folderPath, ImageName);
                        ImagePath = folderPath + ImageName;
                        bo.ImagePath = ImagePath;
                        bo.AdID = strResult;
                        bo.IsImage = true;
                        bo.IsVideo = false;
                        bll.Insert_AdImageVideosPath(bo);
                    }
                    UploadDynamicImages(strResult);
                 //   UploadDynamicVideos(strResult);
                    lblMsg.Text = "Your Ad is posted successfully, to view your add <a href='/Classified/Cld-Ad-Details.aspx?AdID=" + strResult.ToString() + "' style='color:#01a489'>click here.</a>";
                    reset();
                }
                else
                {
                    lblMsg.Text = "Some error occured please try after some time";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }
    }

 
    void reset()
    {
        txtTitle.Text = string.Empty;  
        txtLocal.Text = string.Empty;
        txtDescription.Text = string.Empty;
        ddlCategory.SelectedIndex = 0;
        ddlSubCategory.SelectedIndex = 0;
        chkFeaturedAd.Checked = false;
        chkTopAd.Checked = false;
        chkHomepageGallery.Checked = false;
    }

    #region for Validation

    protected bool ValidatePage()
    {
        bool IsValidated = true;
        if (MandatoryFieldsCheck())
        {
            if (!PatternCheck())
            {
                IsValidated = false;
            }
        }
        else
        {
            IsValidated = false;
        }
        return IsValidated;
    }

    protected bool MandatoryFieldsCheck()
    {
        if (ddlCategory.SelectedItem.Text == "Select"){
            lblMsg.Text = "Required field - Category";
            return false;
        }

        if (ddlSubCategory.SelectedItem.Text == "Select"){
            lblMsg.Text = "Required field - Sub Category";
            return false;
        }
        if (txtTitle.Text == string.Empty)
        {
            lblMsg.Text = "Required field - Title";
            return false;
        }
         

        //if (FileUpload1.PostedFile == null && FileUpload1.FileName == string.Empty){
        //    lblMsg.Text = "Required field - Image";
        //    return false;
        //} 

        if (txtState.Text == string.Empty)
        {
            lblMsg.Text = "Required field - State";
            return false;
        }
         
         
        if (txtDescription.Text == string.Empty)
        {
            lblMsg.Text = "Required field - Description";
            return false;
        }

         return true;
    }

    protected bool PatternCheck()
    { 
        return true;
    }

    #endregion

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubCategory();
    }

    void UploadDynamicImages(int AdID)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        if ( Request.Files != null)
        {
              HttpFileCollection hfc = Request.Files;
            string[] Uploads = Request.Form.GetValues("ImageFileUpload");
            if (hfc.Count > 0)
            {
                for (int i = 0; i < hfc.Count;i++ )
                {
                    HttpPostedFile value = hfc[i];
                    if (value.ContentLength > 0)
                    {
                        ImageName = Path.GetFileName(value.FileName);
                        folderPath = "AdImages/";
                        ImagePath = folderPath + ImageName;

                        ImageName = busin.ValidateImageName(folderPath, ImageName);
                        ImagePath = folderPath + ImageName;
                        value.SaveAs(Server.MapPath(ImagePath));
                        bo.Path = ImagePath;
                        bo.AdID = AdID;
                        bo.IsImage = true;
                        bo.IsVideo = false;
                        bll.Insert_AdImageVideosPath(bo);
                    }
                }
            } 

        }

    }


    protected void UploadDynamicVideos(int AdID)
    {
        if (Request.Form.GetValues("VideoTextBox") != null)
        {
            string[] Email = Request.Form.GetValues("VideoTextBox");
            if (Email.Length > 0)
            {
                foreach (string value in Email)
                {
                    if (value != string.Empty)
                    {
                        bo.Path = value;
                        bo.AdID = AdID;
                        bo.IsImage = false;
                        bo.IsVideo = true;
                        bll.Insert_AdImageVideosPath(bo);
                    }
                }
            }

        }
    }

    string  GetImagePath()
    {
        if ( Request.Files != null)
        {
            HttpFileCollection hfc = Request.Files;
            string[] Uploads = Request.Form.GetValues("ImageFileUpload");
            if (hfc.Count > 0)
            {
                for (int i = 0; i < hfc.Count;i++ )
                {
                    HttpPostedFile value = hfc[i];
                    if (value.ContentLength > 0)
                    {
                        ImageName = Path.GetFileName(value.FileName);
                        folderPath = "AdImages/";
                        ImagePath = folderPath + ImageName;

                        ImageName = busin.ValidateImageName(folderPath, ImageName);
                        ImagePath = folderPath + ImageName;
                        value.SaveAs(Server.MapPath(ImagePath));
                        return ImagePath;
                    }
                }
            } 

        }
        return string.Empty;
    }
 
}
